<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 头像区域 -->
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- 登录表单区域 -->
            <t-form class="login_form" :data="loginForm" :rules="rules" @reset="onReset" @submit="onSubmit">
                <!-- 用户名 -->
                <t-form-item name="username">
                    <t-input v-model="loginForm.username" placeholder="">
                        <UserIcon slot="prefix-icon"></UserIcon>
                    </t-input>

                </t-form-item>
                <!-- 密码 -->
                <t-form-item name="password">
                    <t-input type="password" v-model="loginForm.password" placeholder="">
                        <lock-on-icon slot="prefix-icon"></lock-on-icon>
                    </t-input>
                </t-form-item>
                <!-- 按钮区域 -->
                <t-form-item class="btns">
                    <t-button theme="primary" style="margin-right: 15px;" type="submit">登录</t-button>
                    <t-button theme="default" type="reset">重置</t-button>
                </t-form-item>
            </t-form>
        </div>
    </div>
</template>

<script>
import { LockOnIcon, UserIcon } from 'tdesign-icons-vue';

export default {

    components: {
        LockOnIcon,
        UserIcon,
    },
    data() {
        return {
            // 登录表单
            loginForm: {
                username: '',
                password: '',
            },
            rules: {
                username: [
                    { required: true, message: '用户名必填', type: 'error' },
                ],
                password: [
                    { required: true, message: '密码必填', type: 'error' },
                ]
            }
        }
    },

    methods: {
        onReset() {
            this.$message.success('重置成功');
        },
        async onSubmit({ validateResult, firstError }) {
            if (validateResult === true) {
                const { data: res } = await this.$http.post('login', this.loginForm)
                if (res.meta.status !== 200) {
                    this.$message.error('登录失败！')
                } else {
                    this.$message.success('登录成功');
                    // 1. 将登录成功之后的 token 保存到客户端的 sessionStorage（会话期间的存储 - 当前浏览器会话间有效） 中
                    window.sessionStorage.setItem('token', res.data.token)
                    // 2. 通过编程式导航跳转到后台主页 路由地址是 /home
                    this.$router.push('/home')
                }

            } else {
                console.log('Errors: ', validateResult);
                this.$message.warning(firstError);
            }
        },
    },

}
</script>

<style lang="less" scoped>
.login_container {

    background-color: #2b4b6b;
    height: 100%;

    .login_box {

        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);

        .avatar_box {

            height: 130px;
            width: 130px;
            border: 1px solid #eee;
            border-radius: 50%;
            padding: 10px;
            box-shadow: 0 0 10px #ddd;

            position: absolute;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #fff;

            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }

        .login_form {
            position: absolute;
            bottom: 0;
            width: 100%;
            padding: 0 20px;
            box-sizing: border-box;

            .btns {
                margin: 30px 0;
                display: flex;
                justify-content: flex-end;
            }
        }

    }


}
</style>